// pages/paymentCode/paymentCode.js
import {
  postRequest,
  getRequest
} from '../../api/request';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    myIndexHeadImg1: app.globalData.myIndexHeadImg1,
    myIndexHeadImg2: app.globalData.myIndexHeadImg2,
    myPaymentCodeBg: app.globalData.myPaymentCodeBg,
    avatarUrl: "",
    erweimaUrl: "",
    barcodeUrl: "",
    showerweima: false,
    showBarCode: false,
    showModal: false,
    user: {},
    orderNumber: {
      afterSale: "0",
      waitDeliver: "38",
      waitPay: "1",
      waitReceive: "3"
    },
    memberQrCode: "", //动态会员卡二维码
    codeInterval: "",
    intervalNumber: 1800,
  },
  //返回上一页
  backPage() {
    wx.navigateBack({
      delta: 1
    })
  },
  //前往交易记录页
  toRechargeOrderListPage() {
    wx.navigateTo({
      url: '../rechargeOrderList/rechargeOrderList?dataType=1',
    })
  },
  //获取个人信息
  getUserBaseInfo() {
    let that = this;
    getRequest("/api/vip/getUserBaseInfo", {
      memberId: app.myInfo.memberId
    }).then((res) => {
      console.log(res.data.data);
      console.log(that.data);
      if (res.data.data != null) {
        wx.setStorageSync('userInfo', res.data.data);
        if (res.data.data.avatar == '') {
          if (res.data.data.gender == 2) {
            res.data.data.avatar = that.data.myIndexHeadImg2;
          } else {
            res.data.data.avatar = that.data.myIndexHeadImg1;
          }
        }
        that.setData({
          user: res.data.data,
        })
      }

    }).catch((err) => {

    });
  },

  //获取动态会员卡二维码(图片流)
  getMemberQrCode() {
    let that = this;
    that.setData({
      memberQrCode: app.API_HOST + '/api/vip/memberQrCode?beid=' + app.beid + '&memberId=' + app.myInfo.memberId + '&' + Math.random() / 9999,
    });
    console.log(this.data.memberQrCode);
  },
  getMemberQrCodeInterval() {
    let that = this;
    this.getMemberQrCode();
    let interval = setInterval(function () {
      let intervalNumber = that.data.intervalNumber - 1;
      if (intervalNumber == -1) {
        that.setData({
          intervalNumber: 1800,
        });
      } else {
        that.setData({
          intervalNumber: intervalNumber,
        });
      }
      if (that.data.intervalNumber == 0) {
        that.getMemberQrCode();
      }
    }, 1000)
    this.setData({
      codeInterval: interval,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      intervalNumber: 1800,
    });
    //如果没有token,则需要重新登录获取个人信息等，避免缺少id导致数据无法获取
    if (app.token == "" || app.token == undefined) {
      app.initLogin().then(res => {
        this.getUserBaseInfo();
        this.getMemberQrCodeInterval();
      }).catch(err => {})
    } else {
      this.getUserBaseInfo();
      this.getMemberQrCodeInterval();
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    let that = this;
    clearInterval(that.data.codeInterval);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    let that = this;
    clearInterval(that.data.codeInterval);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})